﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using Sharp.DataTransferObject;
using Sharp.Infrastructure;
using Sharp.Infrastructure.WebApiClient;
using Sharp.Portal.ExtNet;
using Sharp.Portal.Mvc;
using Sharp.ServiceContracts;
using Sharp.WebBooter.Areas.Demo.Models;
using PartialViewResult = Ext.Net.MVC.PartialViewResult;
using UserIndexViewModel = Sharp.WebBooter.Areas.Demo.Models.UserIndexViewModel;

namespace Sharp.WebBooter.Areas.Demo.Controllers
{
    [AutomaticTemplate]
    public class TabController : Controller
    {
        public ActionResult Index(string containerId)
        {
            return new PartialViewResult
            {
                //ViewName = "Index",
                Model = new UserIndexViewModel(),
                ContainerId = containerId,
                RenderMode = RenderMode.AddTo
            };
        }

        /// <summary>
        ///     分页数据
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetPaged(ExtRequestParameters parameters)
        {
            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                var pageInfo = proxy.Channel.GetUsersPaged(parameters.ToPagingFeature());

                return this.Store(pageInfo.Data, pageInfo.TotalRecords);
            }
        }

        public ActionResult Details(int? id)
        {
            UserDTO userDto;
            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                userDto = proxy.Channel.GetUserByKey(id.Value).Data;
            }
 
            var vm = userDto.To<UserViewModel>();
            userDto.Password = string.Empty;
           

            return new PartialViewResult { Model = vm };
        }

        public ActionResult Create()
        {
            var vm = new UserViewModel();
 

            return new PartialViewResult {Model = vm};
        }
    }
}